package com.jason.platform.supervision.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.activity.StatisticsActivity;
import com.jason.platform.supervision.adapter.StatisticVisitAdapter;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.Category;
import com.jason.platform.supervision.beans.StatisticsVisitResp;
import com.jason.platform.supervision.beans.WorkerBean;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import org.kymjs.aframe.http.KJFileParams;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StatisticsVisitFragment
 * @Description: 走访次数
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-28 10:41:54
 */
public class StatisticsVisitFragment extends BaseFragment {

    private static final String TAG = StatisticsVisitFragment.class.getSimpleName();

    private Category mParentCategory;
    private int mPosition;

    private ListView mListView;
    private StatisticVisitAdapter mAdapter;
    private StatisticsActivity mActivity;
    private View mListHeader;
    private View mFootView;

    private List<WorkerBean> mWorkerBeans = new ArrayList<WorkerBean>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate...");
        mParentCategory = (Category) tag;
        if (arg1 != null) {
            mPosition = (Integer) arg1;
        }
        mActivity = (StatisticsActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG,"onCreateView...");
        View view = inflater.inflate(R.layout.fragment_statistics_visit, null, true);
        mListView = (ListView) view.findViewById(R.id.statistics_visit_list);
        mListView.addHeaderView(initListHeader(inflater));
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(mActivity, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.GONE);
        mListView.addFooterView(mFootView);
        mAdapter = new StatisticVisitAdapter(mActivity);
        mListView.setAdapter(mAdapter);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {//滚动到底部
                        if (!mIsLoading && mHasNextPage) {
                            loadMore();
                        } else {
                            if (mFootView != null) {
                                mFootView.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        return view;
    }

    private View initListHeader(LayoutInflater inflater){
        mListHeader = inflater.inflate(R.layout.list_header_statistics_visit, null, true);
        return mListHeader;
    }

    private void loadMore() {
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(mActivity, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.VISIBLE);
        mListView.setSelection((mListView.getAdapter().getCount() - 1));
        mIsLoading = true;

        KJFileParams mParams = new KJFileParams();
        mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        mParams.put("organId", SharedPreferencesUtils.getOrganId(mActivity).toString());
        Task task = mActivity.getStatisticsVisitTask(Constant.Manager.GET_WORK_TONGJI, mParams);
        task.mIsStart = false;
        task.execute();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume...");
    }

    @Override
    public void onFrameResume() {
        super.onFrameResume();
        Log.i(TAG,"onResume...");
    }

    @Override
    public void onDataCallback(Task task) {
        super.onDataCallback(task);
        if (task.type == Constant.Manager.GET_WORK_TONGJI){
            if (task.isSuccess) {
                StatisticsVisitResp resp = (StatisticsVisitResp) task.resData;
                if (task.mIsStart){
                    mWorkerBeans.clear();
                }
                mWorkerBeans.addAll(resp.getValues());
                if (mWorkerBeans != null && !mWorkerBeans.isEmpty()){
                    if (mAdapter != null){
                        mAdapter.addAll(mWorkerBeans);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                mHasNextPage = !resp.isLastPage();
                mIsLoading = false;
                if (mHasNextPage) {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.GONE);
                    }
                }
                setNetInitState(BaseFragment.NetState.INIT_SUCCESS);
            }else {
                if (mAdapter != null){
                    mAdapter.addAll(new ArrayList<WorkerBean>());
                    mAdapter.notifyDataSetChanged();
                }
                if (mFootView != null) {
                    mFootView.setVisibility(View.GONE);
                }
                Toast.makeText(mActivity,task.rspDesc,Toast.LENGTH_SHORT).show();
                setNetInitState(NetState.INIT_FAILED);
            }
        }
    }
}
